<template>
  <div>
    <el-card shadow="never">
      <div class="login-container">
        <el-form label-width="80px" style="width:80%">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="/api/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="user.username" clearable placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="user.password" clearable type="password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="user.nickName" clearable placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="user.email" clearable placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="user.phone" clearable placeholder="请输入电话" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="registerBtn">注册</el-button>
            <el-button style="margin-left:8px;" @click="$router.go(-1)">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login, resgiter } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      user: {},
      imageUrl: ''
    }
  },
  created() {
  },
  methods: {
    registerBtn() {
      resgiter(this.user).then(resp => {
        this.$notifySuccess(resp.msg)
        this.$router.push('/login')
      })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 jpg 或者 png 格式!')
      }
      return isJPG
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.user.avatar = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container{
    margin-top: 40px;
    height: 100vh;
    display: flex;
    justify-content: center;
}

</style>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 50%;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
